﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;
using RockCompany.Master.Validator;

namespace RockCompany.Master.View
{
    public partial class SI01 : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SI01FormModel frmModel;
        private string sUserName;
        private bool isChooser = false;
        private bool isMat = false;
        private bool isUpdateMode = false;
        private int prevSelectedIndex = -1;
        private int chooserType = SI01Const.callChooseNVL;
        #endregion

        #region Constructors
        public SI01() //For testing
        {
            InitializeComponent();

            trans = new Translator();
            trans.controlTranslate(this);

            initDefaultData();

            uAuth = new UserAuthenticate();
            uAuth.setRights(this, "tth");
        }

        public SI01(Translator trans, string sUName, Form caller)
            : base(sUName, caller)
        {
            InitializeComponent();

            this.trans = trans;
            trans.controlTranslate(this);

            initDefaultData();

            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUName);

            sUserName = sUName;
        }
        #endregion

        #region Event functions
        private void button_SearchMat_Click(object sender, EventArgs e)
        {
            loadDataGrid();
        }

        private void button_NewMat_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                if (SI01FormController.newMatValidation(textBox_NewMatNo.Text, textBox_NewMatPrice.Text))
                {
                    NGUYEN_VLIEU_VTU_DTO newMat = new NGUYEN_VLIEU_VTU_DTO();

                    newMat.MA_NVL_VTU = textBox_NewMatNo.Text;
                    newMat.TEN_LOAI_NVL_VTU = comboBox_NewMatType.Text;
                    newMat.TEN_NVL_VTU = textBox_NewMatName.Text;
                    newMat.DON_VI = comboBox_NewMatUnit.Text;
                    newMat.QUY_CACH = textBox_NewMatCom.Text;
                    newMat.DON_GIA = Util.parseDouble(textBox_NewMatPrice.Text);
                    newMat.TY_GIA = comboBox_TyGia.Text;
                    newMat.CREATION_USER = sUserName;
                    newMat.NVL_FLAG = isMat;

                    if (frmModel.insertNewMat(newMat))
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        loadDataGrid();
                        comboBox_SearchMatType.DataSource = frmModel.matTypeLOV();
                        comboBox_NewMatUnit.DataSource = frmModel.matUnitLOV();
                        if (comboBox_NewMatType.Enabled)
                            comboBox_NewMatType.DataSource = frmModel.matTypeLOV();
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
                else
                {
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.NewInforInvalid), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_ModMat_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_MODIFY)))
            {
                string s1 = UserValidator.isValidNumer(textBox_NewMatPrice.Text);
                if (!string.IsNullOrEmpty(s1))
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s1), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
                else
                {
                    NGUYEN_VLIEU_VTU_DTO updateMat = new NGUYEN_VLIEU_VTU_DTO();
                    updateMat.MA_NVL_VTU = textBox_NewMatNo.Text;
                    updateMat.TEN_LOAI_NVL_VTU = comboBox_NewMatType.Text;
                    updateMat.TEN_NVL_VTU = textBox_NewMatName.Text;
                    updateMat.DON_VI = comboBox_NewMatUnit.Text;
                    updateMat.QUY_CACH = textBox_NewMatCom.Text;
                    updateMat.DON_GIA = Util.parseDouble(textBox_NewMatPrice.Text);
                    updateMat.TY_GIA = comboBox_TyGia.Text;
                    updateMat.LAST_UPDATE_USER = sUserName;
                    updateMat.NVL_FLAG = isMat;

                    isUpdateMode = !frmModel.updateMat(updateMat);
                    if (!isUpdateMode)
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        loadDataGrid();
                        prevSelectedIndex = -1;
                        frmModel.setSelectedRow(prevSelectedIndex);
                        textBox_NewMatNo.ReadOnly = isUpdateMode; textBox_NewMatNo.Text = string.Empty;
                        button_ModMat.Enabled = isUpdateMode;
                        button_NewMat.Enabled = !isUpdateMode;
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void dataGridView_Mats_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView_Mats.Columns[NGUYEN_VLIEU_VTU_DTO.N_CHON].Index)
            {
                frmModel.addRemoveCheckedList(e.ColumnIndex, frmModel.getCheckedRow(e.RowIndex));
            }
        }

        private void button_DelMat_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (frmModel.removeSelectedMats())
                {
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    loadDataGrid();
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                frmModel.DataList.Clear();
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_ChooseMat_Click(object sender, EventArgs e)
        {
            if (isChooser && frmModel.DataList.Count > 0)
            {
                this.Hide();
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_Mats.RowCount > 0)
            {
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {
                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this)))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void checkBox_IsMat_CheckedChanged(object sender, EventArgs e)
        {
            isMat = checkBox_IsMat.Checked;
        }

        private void dataGridView_Mats_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView_Mats.CurrentRow != null)
            {
                if (prevSelectedIndex == -1)
                {
                    prevSelectedIndex = dataGridView_Mats.CurrentRow.Index;
                    isUpdateMode = (prevSelectedIndex == dataGridView_Mats.CurrentRow.Index);
                }
                else
                {
                    isUpdateMode = (prevSelectedIndex != dataGridView_Mats.CurrentRow.Index);
                }
                textBox_NewMatNo.ReadOnly = isUpdateMode;
                button_ModMat.Enabled = isUpdateMode;
                button_NewMat.Enabled = !isUpdateMode;
                checkBox_IsMat.Enabled = !isUpdateMode;
                if (isUpdateMode)
                {
                    frmModel.setSelectedRow(dataGridView_Mats.CurrentRow.Index);
                    textBox_NewMatNo.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU].EditedFormattedValue);
                    textBox_NewMatName.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU].EditedFormattedValue);
                    textBox_NewMatPrice.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_DON_GIA].EditedFormattedValue);
                    textBox_NewMatCom.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_QUY_CACH].EditedFormattedValue);
                    comboBox_NewMatUnit.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_DON_VI].EditedFormattedValue);
                    comboBox_NewMatType.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU].EditedFormattedValue);
                    comboBox_TyGia.Text = Util.parseString(dataGridView_Mats.CurrentRow.Cells[NGUYEN_VLIEU_VTU_DTO.N_TY_GIA].EditedFormattedValue);
                    prevSelectedIndex = dataGridView_Mats.CurrentRow.Index;
                }
                else
                {
                    textBox_NewMatNo.Text = string.Empty;
                    textBox_NewMatName.Text = string.Empty;
                    textBox_NewMatPrice.Text = string.Empty;
                    textBox_NewMatCom.Text = string.Empty;
                }
            }
        }
        
        #endregion

        #region Other functions
        private void initDefaultData()
        {
            frmModel = new SI01FormModel();
            frmModel.Owner = this;
            frmModel.translator = this.trans;
            SI01FormModel.bindCombo(comboBox_SearchMatType, NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU, NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU, frmModel.matTypeLOV());
            SI01FormModel.bindCombo(comboBox_NewMatUnit, NGUYEN_VLIEU_VTU_DTO.N_DON_VI, NGUYEN_VLIEU_VTU_DTO.N_DON_VI, frmModel.matUnitLOV());
            if (comboBox_NewMatType.Enabled)
                SI01FormModel.bindCombo(comboBox_NewMatType, NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU, NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU, frmModel.matTypeLOV());

            NGUYEN_VLIEU_VTU_DTO temp = new NGUYEN_VLIEU_VTU_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_CHON));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU));           
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_DON_GIA));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TY_GIA));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_DON_VI));
            properties.Add(temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_QUY_CACH));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_DON_GIA);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_TY_GIA);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_DON_VI);
            readOnlyCols.Add(NGUYEN_VLIEU_VTU_DTO.N_QUY_CACH);

            frmModel.buildDataGrid(dataGridView_Mats, properties, readOnlyCols);
        }

        private void loadDataGrid()
        {
            if (chooserType == SI01Const.callChooseVTU)
                dataGridView_Mats.DataSource = frmModel.searchNotMat();
            else
                dataGridView_Mats.DataSource = frmModel.searchMaterial(comboBox_SearchMatType.Text, textBox_SearchMatNo.Text);
        }

        public void callChooser(bool b) 
        { 
            isChooser = b; 
            button_ChooseMat.Enabled = b;
            button_DelMat.Enabled = !b;
            button_ModMat.Enabled = !b;
            button_NewMat.Enabled = !b;
        }

        public void callChooser(int type)
        {
            if (type == SI01Const.callChooseVTU)
            {
                callChooser(true);
                comboBox_SearchMatType.Enabled = false;
                textBox_SearchMatNo.ReadOnly = true;
                chooserType = type;
            }
        }

        public DataTable getSelectedMatsNo()
        {
            return frmModel.getSelectedMats();
        }
        #endregion        
    }
}
